﻿using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace WpfExample01.View.ProgressBar
{
    /// <summary>
    /// ProgressBarExample01.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressBarExample01 : Window
    {
        readonly Window parentWindow = null;

        public ProgressBarExample01(Window parentWindow)
        {
            InitializeComponent();
            this.Show();

            this.parentWindow = parentWindow;
            parentWindow.Hide();
        }
    
        public void ReturnToParentWindow(object sender, RoutedEventArgs e)
        {
            parentWindow.Show();
            this.Close();
        }

        private async void StartAllButton_Click(object sender, RoutedEventArgs e)
        {
            await Task.Run(() =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    Dispatcher.Invoke(() => progressBar.Value = i);
                    // 模拟任务执行的时间
                    System.Threading.Thread.Sleep(50); 
                }
            });

            MessageBox.Show("数据处理完成！");
        }

        private void StartStepButton_Click(object sender, RoutedEventArgs e)
        {
            double currentProgress = progressBar.Value;
            double maximum = progressBar.Maximum;
            double step = 10;
            double newProgress = currentProgress + step;
            if (newProgress >= maximum)
            {
                newProgress = maximum;
            }

            for (double i = currentProgress; i <= newProgress; i++)
            {
                progressBar.Value = i;
                // 模拟任务执行的时间
                System.Threading.Thread.Sleep(100); 
            }

            if (newProgress >= maximum)
            {
                MessageBox.Show("数据处理完成！");
            }
        }
    }
}
